﻿#include "uimodelyxtableview.h"
UiModelYxTableView::UiModelYxTableView(QWidget *parent)
    : UiFilterTableView{parent}
{
    connect(m_addBtn, &QPushButton::clicked, this, &UiModelYxTableView::addYx);
}

void UiModelYxTableView::reload(const QString &modelname, const QString &user) {
    this->clearData();

    if(modelname.isEmpty()) {
        m_modelName.clear();
        m_user.clear();
        return;
    }

    m_user = user;
    m_modelName = modelname;

    QList<QSharedPointer<IModel>> yxs;
    if(!CWebRequestApi::ins().getModelYxList(yxs, modelname))
        return;

    if(yxs.size() > 0) {
        this->appendData(yxs);
    }
}

void UiModelYxTableView::addYx() {
    if(m_modelName.isEmpty()) {
        ElaMessageBar::error(ElaMessageBarType::Top, "警告", "请先选择操作模型!",2000);
        return;
    }

    QSharedPointer<CRtModelYxInfo> info(new CRtModelYxInfo);

    int max_sn = 0;
    QStringList groups;
    QStringList names;
    for(int row = 0; row < m_model->rowCount(); ++row) {
        CRtModelYxInfo* yxptr = static_cast<CRtModelYxInfo*>(m_model->getDataByIndex(row).data());
        if(!yxptr)
            continue;

        if(!groups.contains(yxptr->grouping))
            groups.append(yxptr->grouping);

        if(!names.contains(yxptr->name))
            names.append(yxptr->name);

        if(yxptr->sn >= max_sn)
            max_sn = yxptr->sn;
    }

    info->model_name = m_modelName;
    info->user = m_user;
    info->sn = max_sn+1;
    info->name = "";

    UiModelSigBaseEdit editw(info, groups,UiModelSigBaseEdit::ECreate);

    CYxValueEditPlugin* editbox = new CYxValueEditPlugin;
    QString curValue = info->getFieldValueByFielddName(yxColFieldName(YX_ENUM::EValues)).toString();
    if(!editbox->parseData(curValue)) {
        ElaMessageBar::error(ElaMessageBarType::Top, "系统错误", "遥信值编辑插件加载失败!",2000);
        return;
    }

    QVBoxLayout* mainlayout = new QVBoxLayout;
    QVBoxLayout* layout = new QVBoxLayout;

    QGroupBox box;
    box.setTitle("遥信值编辑");
    layout->addWidget(editbox);
    box.setLayout(layout);

    mainlayout->addWidget(&editw);
    mainlayout->addWidget(&box);

    UiCustomDialog* dialog = new UiCustomDialog;
    dialog->resize(500, 400);
    dialog->setConten(mainlayout);
    while(dialog->exec() == UiCustomDialog::Accepted) {
        editw.save();

        if(!editbox->checkData()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "值映射重复或者值描述为空!",2000);
            continue;
        }

        if(names.contains(info->name)){
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信号名字重复,请重新输入!",2000);
            continue;
        }

        if(info->name.isEmpty() || info->descr.isEmpty() || info->grouping.isEmpty()) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "信息不能为空,请检查!",2000);
            continue;
        }

        if(editbox->vcount() > 0) {
            QString buf = editbox->readData();
            QJsonDocument doc = QJsonDocument::fromJson(buf.toUtf8());

            QJsonObject jsonvalues = doc.object();
            info->valuesFromJson(jsonvalues["valuesmap"].toArray());
        }

        if(!CWebRequestApi::ins().addYx(info)) {
            ElaMessageBar::error(ElaMessageBarType::Top, "错误", "添加遥信失败",2000);
            break;
        }

        this->appendData(info);
        ElaMessageBar::success(ElaMessageBarType::Top, "完成", "添加遥信成功",1500);
        break;
    }
    dialog->deleteLater();
}

void UiModelYxTableView::slotDeleteSig(const QList<QSharedPointer<IModel> > &dataList)
{
    QStringList failds;
    if(!CWebRequestApi::ins().removeSig(dataList, failds, ESigType::EYx)) {
        ElaMessageBar::error(ElaMessageBarType::Top, "错误", "删除遥信失败",2000);
        return;
    }

    QList<QSharedPointer<IModel>> list;
    if(failds.size() > 0) {
        QString msg;
        for(auto &it : failds) {
            msg+=it;
            msg+=",";
        }
        ElaMessageBar::error(ElaMessageBarType::Top, "错误", "这些遥信删除失败："+msg,2000);

        for(auto& it : dataList) {
            QString delname = it->getFieldValueByFielddName(baseSigColFieldName(BASE_SIG_ENUM::EName)).toString();
            if(!failds.contains(delname)) {
                list.append(it);
            }
        }
        m_model->removeData(list);
    } else {
        m_model->removeData(dataList);
    }
}
